home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 134_01.zip / CASM2.C < prev    next >
Text File  |  1993-06-12  |  24KB  |  945 lines

  1. /*
  2.     CASM.C    -- written by Leor Zolman, 2/82
  3.     Modified for v1.50 11/14/82
  4.  
  5.     Modified for use with CTOA CRL-to-ASM postprocessor 11/2/83 K. Kenny
  6.     Modified to allow conditional assembly of relocatable code
  7.         11/15/83 K. Kenny.
  8.  
  9.     CP/M ASM preprocessor: renders MAC.COM and CMAC.LIB unnecessary.
  10.  
  11.     See the CASM Appendix in the User's Guide for complete information.
  12.  
  13.     Compile and link with:
  14.  
  15.         cc casm.c -o -e5000
  16.  
  17.         l2 casm
  18.     (or)    clink casm
  19. */
  20.  
  21. #include <bdscio.h>
  22.  
  23. #define TITLE "BD Software CRL-format ASM Preprocessor v1.50+mods\n"
  24.  
  25.  
  26. /*
  27.  *    Customizable definitions:
  28.  */
  29.  
  30. #define DEFUSER    "0/"        /* default user area for include files    */
  31.                 /* make this a null string for "current" */
  32.  
  33. #define DEFDISK "A:"        /* default disk for include files    */
  34. #define CASMEXT    ".CSM"        /* extension on input files     */
  35. #define ASMEXT    ".ASM"        /* extension on output files    */
  36. #define SUBFILE "A:$$$.SUB"    /* Submit file to erase on error. To not */
  37.                 /* erase any, use a null string ("")     */
  38.  
  39. #define CONTROL_C 3        /* Abort character */
  40. #define    EQUMAX    500        /* maximum number of EQU ops    */
  41. #define FUNCMAX    100        /* maximum number of functions  */
  42. #define NFMAX    100        /* maximum number of external
  43.                    functions in one function     */
  44. #define LABMAX    150        /* max number of local labels in one func */
  45. #define TXTBUFSIZE 2000        /* max # of chars for labels and needed
  46.                    function names for a single function    */
  47. #define CONDBUFSIZE 2000    /* max # of chars of conditional assembly 
  48.                    pseudo-ops per function */
  49.  
  50. /*
  51.  *    End of customizable section
  52.  */
  53.  
  54. #define DIRSIZE    512        /* max # of byte in CRL directory     */
  55. #define TPALOC    0x100        /* base of TPA in your system     */
  56.  
  57.         /* Global data used throughout processing
  58.            of the input file:            */
  59.  
  60. char    fbuf[BUFSIZ];        /* I/O buffer for main input file    */
  61. char    incbuf[BUFSIZ];        /* I/O buffer for included file     */
  62. char    obuf[BUFSIZ];        /* I/O buffer for output file        */
  63.  
  64. char    *cbufp;            /* pointer to currently active input buf */
  65. char    *cfilnam;        /* pointer to name of current input file */
  66. char    nambuf[30],        /* filenames for current intput */
  67.     nambuf2[30],        /* and output files.        */
  68.     onambuf[30];
  69.  
  70. char    *equtab[EQUMAX];    /* table of absolute symbols    */
  71. int    equcount;        /* # of entries in equtab    */
  72.  
  73. char    *fnames[FUNCMAX];    /* list of functions in the source file */
  74. int    fcount;            /* # of entries in fnames        */
  75.  
  76. int    lino,savlino;        /* line number values used for error     */
  77.                 /* reporting.                */
  78.  
  79. char    doingfunc;        /* true if currently processing a function */
  80.  
  81. char    errf;            /* true if an error has been detected    */
  82. char    verbose;        /* true to insert wordy comments in output */
  83. char    careful;        /* true to detect old CMAC.LIB macros   */
  84. char    blankflag;        /* true if last line processed was null */
  85.  
  86.         /* Global data used during the processing of a
  87.            single function in the source file:        */
  88.  
  89. char    *nflist[NFMAX];        /* list of needed functions for a function */
  90. int    nfcount;        /* number of entries in nflist    */
  91.  
  92. struct {
  93.     char *labnam;        /* name of function label */
  94.     char defined;        /* whether it has been defined yet */
  95. } lablist[LABMAX];
  96.  
  97. int    labcount;        /* number of local labels in a function */
  98.  
  99. char    txtbuf[TXTBUFSIZE],    /* where text of needed function names    */
  100.     *txtbufp;        /* and function labels go        */
  101.  
  102. char    condbuf[CONDBUFSIZE];    /* where conditional assembly directives go */
  103. union {
  104.     int * ci;
  105.     char * cb;
  106.     } condptr;
  107.  
  108. char     linbuf[150],        /* text line buffers    */
  109.     linsav[150],
  110.     workbuf[150],
  111.     undrbuf[150],
  112.     pbuf[150], *pbufp;
  113.  
  114. char    *cfunam;        /* pointer to name of current function */
  115. int    relblc;            /* relocation object count for a function */
  116.  
  117. char    pastnfs;        /* true if we've passed all needed function */
  118.                 /* declarations ("external" pseudo ops)        */
  119.  
  120. int    argcnt;            /* values set by the "parse_line" function */
  121. char    *label,
  122.     *op,
  123.     *argsp,
  124.     *args[40];
  125.  
  126. char     *gpcptr;        /* general-purpose text pointer    */
  127.  
  128. /*
  129.  * Open main input file, open output file, initialize needed globals
  130.  * and process the file:
  131.  */
  132.  
  133. main(aarghc,aarghv)
  134. char **aarghv;
  135. {
  136.     int i,j,k;
  137.     char c, *inpnam, *outnam;
  138.  
  139.     puts(TITLE);
  140.  
  141.     initequ();        /* initialize EQU table with reserved words */
  142.     fcount = 0;        /* haven't seen any functions yet */
  143.     doingfunc = FALSE;    /* not currently processing a function */
  144.     errf = FALSE;        /* no errors yet */
  145.     verbose = careful = FALSE;
  146.     inpnam = outnam = NULL;        /* haven't seen any names yet */
  147.     blankflag = FALSE;    /* haven't just processed a null line */
  148.     
  149.     while (--aarghc) 
  150.     {
  151.         ++aarghv;        /* bump to next arg text */
  152.         if (**aarghv == '-')
  153.         {
  154.             switch(c = aarghv[0][1])
  155.             {
  156.             case 'F':
  157.                 careful = 1;
  158.                 break;
  159.  
  160.             case 'C':
  161.                 verbose = 1;
  162.                 break;
  163.  
  164.             case 'O':
  165.                 if (aarghv[0][2])
  166.                     outnam = &aarghv[0][2];
  167.                 else if (--aarghc)
  168.                     outnam = *++aarghv;
  169.                 else goto usage;
  170.                 break;
  171.  
  172.             default: goto usage;
  173.             }
  174.         }
  175.         else
  176.             inpnam = *aarghv;
  177.     }
  178.  
  179.     if (!inpnam) {
  180.   usage:    puts("Usage:\tcasm [-f] [-c] [-o <name>] <filename>\n");
  181.         puts("-F: flag old CMAC.LIB macros if spotted\n");
  182.         puts("-C: don't strip comments from input and output\n");
  183.         puts("-O <name>: Call the output file <name>.ASM\n");
  184.         exit();
  185.     }
  186.  
  187.                 /* set up filenames with proper extensions: */
  188.     for (i = 0; (c = inpnam[i]) && c != '.'; i++)
  189.         nambuf[i] = c;
  190.     nambuf[i] = '\0';
  191.  
  192.     strcpy(onambuf, outnam ? outnam : nambuf);
  193.     strcat(nambuf,CASMEXT);        /* input filename */
  194.     cbufp = fbuf;            /* buffer pointer */
  195.     cfilnam = nambuf;        /* current filename pointer */
  196.     if (fopen(cfilnam,cbufp) == ERROR)
  197.         exit(printf("Can't open %s\n",cfilnam));
  198.  
  199.     if (!hasdot(onambuf))
  200.         strcat(onambuf,ASMEXT);        /* output filename */
  201.     if (fcreat(onambuf,obuf) == ERROR)
  202.         exit(printf("Can't create %s\n",onambuf));
  203.  
  204.                     /* begin writing output file */
  205.     fprintf2(obuf,"\nTPALOC\t\tEQU\t%04xH\n",TPALOC);
  206.  
  207.     fprintf2(obuf,"\nSYS$EXTFLAG\tSET\t0\n");
  208.     fprintf2(obuf,"SYS$EXTADDR\tSET\t0\n");
  209.     fprintf2(obuf,"SYS$EXTSIZE\tSET\t0\n");
  210.     fprintf2(obuf,"\t\tORG\tTPALOC+0205H\n\n");
  211.  
  212.     lino = 1;            /* initialize line count */
  213.  
  214.     while (get_line()) {        /* main loop */
  215.         if (kbhit() && getchar() == CONTROL_C)
  216.             abort("Aborted by ^C\n");
  217.         process_line();        /* process lines till EOF */
  218.         lino++;
  219.     }
  220.  
  221.     if (doingfunc)            /* if ends inside a function, error */
  222.         abort("File ends, but last function is unterminated\n");
  223.  
  224.     if (errf)
  225.     {
  226.         puts("Fix those errors and try again...");
  227.         unlink(onambuf);
  228.         if (*SUBFILE) 
  229.             unlink(SUBFILE);
  230.     }
  231.     else
  232.     {
  233.                             /* end of functions */
  234.         fputs2("\nEND$CRL\t\tEQU\t$-TPALOC\n",obuf);
  235.         fputs2("SECTORS$ EQU ($-TPALOC)/256+1 ;USE FOR \"SAVE\" !.\n",
  236.             obuf);
  237.         putdir();            /* now put out CRL directory */
  238.         fputs2("\t\tEND\n",obuf);    /* end of ASM file */
  239.         putc(CPMEOF,obuf);        /* CP/M EOF character */
  240.         fclose(cbufp);            /* close input file */
  241.         fclose(obuf);            /* close output file */
  242.         printf("%s is ready to be assembled.\n",onambuf);
  243.     }
  244. }
  245.  
  246. /*
  247.  * Get a line of text from input stream, and process
  248.  * "include" ops on the fly:
  249.  */
  250.  
  251. int get_line()
  252. {
  253.     int i;
  254.  
  255. top:    if (!fgets(linbuf,cbufp)) {        /* on EOF: */
  256.         if (cbufp == incbuf) {        /* in an "include" file? */
  257.             fabort(cbufp->_fd);        /* close the file */
  258.             cbufp = fbuf;        /* go back to mainline file */
  259.             cfilnam = nambuf;
  260.             lino = savlino + 1;
  261.             return get_line();
  262.         }
  263.         else return NULL;
  264.     }
  265.  
  266.     if (!verbose)                /* strip commments, default */
  267.     {
  268.         for (i = 0; linbuf[i]; i++)
  269.         {
  270.             if (linbuf[i] == ';')
  271.             {
  272.                 while (i && isspace(linbuf[i-1]))
  273.                     i--;
  274.                 if (!i && blankflag)
  275.                 {
  276.                     lino++;
  277.                     goto top;
  278.                 }
  279.                 strcpy(&linbuf[i], "\n");
  280.                 blankflag = TRUE;
  281.                 break;
  282.             }
  283.             if (linbuf[i] == '\'' || linbuf[i] == '"')
  284.                 break;
  285.         }
  286.         if (!linbuf[i])
  287.             blankflag = FALSE;        
  288.     }
  289.  
  290.     parse_line();                /* not EOF. Parse line */
  291.     if (streq(op,"INCLUDE")  ||        /* check for file inclusion */
  292.         streq(op,"MACLIB")) {
  293.         if (cbufp == incbuf)        /* if already in an include, */
  294.          abort("Only one level of inclusion is supported"); /* error */
  295.         if (!argsp)
  296.          abort("No filename specified");
  297.         cbufp = incbuf;            /* set up for inclusion */
  298.         savlino = lino;
  299.  
  300.         for (i = 0; !isspace(argsp[i]); i++)    /* put null after */
  301.             ;                /* filename      */
  302.         argsp[i] = '\0';
  303.  
  304.         *nambuf2 = '\0';
  305.  
  306.         if (*argsp == '<') {        /* look for magic delimiters */
  307.             strcpy(nambuf2,DEFUSER);
  308.             if (argsp[2] != ':')    /* if no explicit disk given */
  309.                 strcat(nambuf2,DEFDISK); /* then use default */
  310.             strcat(nambuf2,argsp+1);
  311.             if (nambuf2[i = strlen(nambuf2) - 1] == '>')
  312.                 nambuf2[i] = '\0';
  313.         } else if (*argsp == '"') {
  314.             strcpy(nambuf2,argsp+1);
  315.             if (nambuf2[i = strlen(nambuf2) - 1] == '"')
  316.                 nambuf2[i] = '\0';
  317.         } else
  318.             strcpy(nambuf2,argsp);
  319.  
  320.         if (fopen(nambuf2,cbufp) == ERROR) {
  321.             if (!hasdot(nambuf2)) {
  322.                 strcat(nambuf2,".LIB");
  323.                 if (fopen(nambuf2,cbufp) != ERROR)
  324.                     goto ok;
  325.             }                
  326.             printf("Can't open %s\n",nambuf2);
  327.             abort("Missing include file");
  328.         }
  329.  
  330.     ok:    lino = 1;
  331.         cfilnam = nambuf2;
  332.         return get_line();
  333.     }
  334.     return 1;
  335. }
  336.  
  337. parse_line()
  338. {
  339.     int i;
  340.     char c;
  341.  
  342.     label = op = argsp = NULL;
  343.     argcnt = 0;
  344.  
  345.     strcpy2(pbuf,linbuf);
  346.     strcpy2(linsav,linbuf);
  347.     pbufp = pbuf;
  348.  
  349.     if (!*pbufp) return;        /* ignore null lines */
  350.     if (!isspace(c = *pbufp)) {
  351.         if (c == ';')
  352.             return;        /* totally ignore comment lines */
  353.         label = pbufp;        /* set pointer to label    */
  354.         while (isidchr(*pbufp))    /* pass over the label identifier */
  355.             pbufp++;
  356.         *pbufp++ = '\0';    /* place null after the identifier */
  357.     }
  358.  
  359.     skip_wsp(&pbufp);
  360.     if (!*pbufp || *pbufp == ';')
  361.         return;
  362.     op = pbufp;            /* set pointer to operation mnemonic */
  363.     while (isalpha(*pbufp))
  364.         pbufp++;          /* skip over the op         */
  365.     if (*pbufp) *pbufp++ = '\0';    /* place null after the op    */
  366.  
  367.  
  368.                     /* now process arguments    */
  369.     skip_wsp(&pbufp);
  370.     if (!*pbufp || *pbufp == ';')
  371.         return;
  372.     argsp = linsav + (pbufp - pbuf);    /* set pointer to arg list */
  373.  
  374.                     /* create vector of ptrs to all args
  375.                        that are possibly relocatable */
  376.     for (argcnt = 0; argcnt < 40;) {
  377.         while (!isidstrt(c = *pbufp))
  378.             if (!c || c == ';')
  379.                 return;
  380.             else
  381.                 pbufp++;
  382.  
  383.         if (isidchr(*(pbufp - 1))) {
  384.             pbufp++;
  385.             continue;
  386.         }
  387.  
  388.         args[argcnt++] = pbufp;            
  389.         while (isidchr(*pbufp)) pbufp++;
  390.         if (*pbufp) *pbufp++ = '\0';
  391.     }
  392.     error("Too many operands in this instruction for me to handle\n");
  393. }
  394.  
  395. process_line()
  396. {
  397.     char *cptr, c;
  398.     int i,j;
  399.  
  400.     if (op) {
  401.             /* check for definitions of global data that will be
  402.                exempt from relocation when encountered in the
  403.                argument field of assembly instructions:        */
  404.  
  405.        if (streq(op,"EQU") || streq(op,"SET") ||
  406.         (!doingfunc &&
  407.             (streq(op,"DS") || streq(op,"DB") || streq(op,"DW"))))
  408.        {
  409.         fputnam(obuf,linbuf);
  410.         cptr = sbrk2(strlen(label) + 1);
  411.         strcpy(cptr,label);
  412.         equtab[equcount++] = cptr;
  413.         if (equcount >= EQUMAX)
  414.             abort(
  415.           "Too many EQU lines...increase 'EQUMAX' and recompile CASM");
  416.         return;
  417.        }
  418.  
  419.        if (streq(op,"EXTERNAL")) {
  420.         if (!doingfunc) abort(
  421.          "'External's for a function must appear inside the function");
  422.         if (pastnfs) error(
  423.          "Externals must all be together at start of function\n");
  424.         for (i = 0; i < argcnt; i++) {
  425.             nflist[nfcount++] = txtbufp;
  426.             strcpy(txtbufp,args[i]);
  427.             bumptxtp(args[i]);
  428.         }
  429.         if (nfcount >= NFMAX) {
  430.           printf("Too many external functions in function \"%s\"\n",
  431.                     cfunam);
  432.           abort("Change the NFMAX constant and recompile CASM");
  433.         }
  434.         return;
  435.        }
  436.  
  437.        if (streq(op,"FUNCTION")) {
  438.  
  439.         if (doingfunc) {
  440.             printf("'Function' op encountered in a function.\n");
  441.             abort("Did you forget an 'endfunc' op?");
  442.         }
  443.         if (!argcnt)
  444.             abort("A name is required for the 'function' op");
  445.  
  446.         cfunam = sbrk2(strlen(args[0]) + 1);
  447.         fnames[fcount++] = cfunam;
  448.         strcpy(cfunam,args[0]);
  449.  
  450.         printf("Processing the %s function...          \r",cfunam);
  451.  
  452.         doingfunc = 1;
  453.         txtbufp = txtbuf;
  454.         labcount = 0;
  455.         nfcount = 0;
  456.         pastnfs = 0;
  457.         condptr.cb = &condbuf; *condptr.ci = 0x7FFF;
  458.  
  459.         if (verbose)
  460.             fprintf2(obuf,"\n\n; The \"%s\" function:\n",cfunam);
  461.  
  462.         fputnam(obuf, cfunam);
  463.         fputs2("$BEG\tEQU\t$-TPALOC\n", obuf);
  464.         return;
  465.        }
  466.  
  467.        if (streq(op,"ENDFUNC") || streq(op,"ENDFUNCTION")) {
  468.         if (!doingfunc)
  469.           abort("'Endfunc' op encountered while not in a function");
  470.  
  471.         if (!pastnfs) flushnfs();    /* flush needed function list */
  472.         fputnam (obuf, cfunam);
  473.         fputs2("$END\tEQU\t$\n", obuf);
  474.         doreloc();             /* flush relocation parameters */
  475.  
  476.         for (i = 0; i < labcount; i++)    /* detect undefined labels */
  477.           if (!lablist[i].defined) {
  478.             printf("The label %s in function %s is undefined\n",
  479.                     lablist[i].labnam,cfunam);
  480.             errf = 1;
  481.           }
  482.         doingfunc = 0;
  483.         return;
  484.        }
  485.     }
  486.  
  487.     if (careful)
  488.     if (streq(op,"RELOC") || streq(op,"DWREL") || streq(op,"DIRECT") ||
  489.         streq(op,"ENDDIR") || streq(op,"EXREL") || streq(op,"EXDWREL") ||
  490.         streq(op,"PRELUDE") || streq(op,"POSTLUDE") || streq(op,"DEFINE"))
  491.         error("Old macro '%s' leftover from \"CMAC.LIB\" days...\n",
  492.                             op);
  493.  
  494.     if (doingfunc && (streq (op, "IF") || streq (op, "ENDIF"))) {
  495.         if (!pastnfs) flushnfs ();
  496.         *condptr.ci++ = relblc;
  497.         strcpy (condptr.cb, linbuf);
  498.         condptr.cb += strlen (condptr.cb) + 1;
  499.         *condptr.ci = 0x7FFF;
  500.         return (fputnam (obuf, linbuf));
  501.         }
  502.  
  503.                 /* No special pseudo ops, so now process
  504.                    the line as a line of assemby code:     */
  505.  
  506.     if (streq(op,"END")) return;        /* don't allow "end" yet     */
  507.  
  508.     if (!doingfunc || (!label && !op))    /* if nothing interesting on */
  509.         return fputnam(obuf,linbuf);    /* line, ignore it    */
  510.     if (!pastnfs)                /* if haven't flushed needed */
  511.         flushnfs();            /* function list yet, do it  */
  512.  
  513.                         /* check for possible label  */
  514.     if (label) {
  515.         sprintf (undrbuf, "%s$L$%s\tEQU\t$-%s$STRT\n",
  516.                 cfunam, label, cfunam);
  517.         fputnam (obuf,undrbuf);
  518.         for (i=0; linbuf[i]; i++)
  519.             if (isspace(linbuf[i]) || linbuf[i] == ':')
  520.                 break;
  521.             else
  522.                 linbuf[i] = ' ';
  523.         if (linbuf[i] == ':') linbuf[i] = ' ';
  524.         for (i = 0; i < labcount; i++)      /* check if in label table */
  525.           if (streq(label,lablist[i].labnam)) {            /* if found, */
  526.             if (lablist[i].defined) {  /* check for redefinition */
  527.                 error("Re-defined label:");
  528.                 printf("%s, in function %s\n",
  529.                         lablist[i].labnam,cfunam);
  530.             }
  531.              else
  532.                 lablist[i].defined = 1;
  533.             goto out;
  534.           }
  535.         lablist[i].labnam = txtbufp;    /* add new entry to */
  536.         lablist[i].defined = 1;        /* label list         */
  537.         strcpy(txtbufp,label);
  538.         bumptxtp(label);
  539.         labcount++;
  540.     }
  541. out:
  542.     if (!op) return fputnam (obuf, linbuf);    /* if label only, all done   */
  543.  
  544.                         /* if a non-relocatable op,  */
  545.     if (norelop(op)) return fputnam(obuf,linbuf);    /* then we're done   */
  546.  
  547.     if (argcnt && doingfunc)
  548.       for (i = 0; i < argcnt; i++) {
  549.         if (gpcptr = isef(args[i]))
  550.            sprintf(workbuf,"%s$EF$%s-%s$STRT",
  551.                 cfunam,gpcptr,cfunam);
  552.         else if (norel(args[i])) continue;
  553.         else {
  554.             sprintf(workbuf,"%s$L$%s",cfunam,args[i]);
  555.             for (j = 0; j < labcount; j++)
  556.                 if (streq(args[i],lablist[j].labnam))
  557.                     goto out2;
  558.             lablist[j].labnam = txtbufp;    /* add new entry to */
  559.             lablist[j].defined = 0;        /* label list         */
  560.             strcpy(txtbufp,args[i]);
  561.             bumptxtp(txtbufp);
  562.             labcount++;
  563.         }           
  564.  
  565.     out2:
  566.         replstr(linbuf, workbuf, args[i] - pbuf, strlen(args[i]));
  567.  
  568.         if (streq(op,"DW")) {
  569.             sprintf (undrbuf, "%s$R%03d\tEQU\t$-%s$STRT\n",
  570.                 cfunam, relblc++, cfunam);
  571.             fputnam (obuf, undrbuf);
  572.             if (argcnt > 1)
  573.               error("Only one relocatable value allowed per DW\n");
  574.         }
  575.         else {
  576.             sprintf (undrbuf,"%s$R%03d\tEQU\t$+1-%s$STRT\n",
  577.                 cfunam, relblc++, cfunam);
  578.             fputnam (obuf, undrbuf);
  579.             }
  580.         break;
  581.       }
  582.     fputnam (obuf, linbuf);
  583. }
  584.  
  585.  
  586. /*
  587.     Test for ops in which there is guanranteed to be no need
  588.     for generation of relocation parameters. Note that the list
  589.     of non-relocatable ops doesn't necessarily have to be complete,
  590.     because for any op that doesn't match, an argument must still
  591.     pass other tests before it is deemed relocatable. This only
  592.     speeds things up by telling the program not to bother checking
  593.     the arguments.
  594. */
  595.  
  596. norelop(op)
  597. char *op;
  598. {
  599.     if (streq(op,"MOV")) return 1;
  600.     if (streq(op,"INR")) return 1;
  601.     if (streq(op,"DCR")) return 1;
  602.     if (streq(op,"INX")) return 1;
  603.     if (streq(op,"DCX")) return 1;
  604.     if (streq(op,"DAD")) return 1;
  605.     if (streq(op,"MVI")) return 1;
  606.     if (streq(op,"DB")) return 1;
  607.     if (streq(op,"DS")) return 1;
  608.     if (op[2] == 'I') {
  609.         if (streq(op,"CPI")) return 1;
  610.         if (streq(op,"ORI")) return 1;
  611.         if (streq(op,"ANI")) return 1;
  612.         if (streq(op,"ADI")) return 1;
  613.         if (streq(op,"SUI")) return 1;
  614.         if (streq(op,"SBI")) return 1;
  615.         if (streq(op,"XRI")) return 1;
  616.         if (streq(op,"ACI")) return 1;
  617.     }
  618.     if (streq(op,"ORG")) return 1;
  619.     if (streq(op,"TITLE")) return 1;
  620.     if (streq(op,"PAGE")) return 1;
  621.     if (streq(op,"EJECT")) return 1;
  622.     if (streq(op,"MACRO")) return 1;
  623.     return 0;
  624. }
  625.  
  626.  
  627. flushnfs()
  628. {
  629.     int i,j, length;
  630.  
  631.     pastnfs = 1;
  632.     relblc = 0;
  633.  
  634.     if (verbose)
  635.         fputs2("\n\n; List of needed functions:\n",obuf);
  636.  
  637.     for (i=0; i < nfcount; i++) {
  638.         strcpy(workbuf,"\t\tDB\t'");
  639.         length = strlen(nflist[i]);
  640.         length = length < 8 ? length : 8;
  641.         for (j = 0; j < length - 1; j++)
  642.             workbuf[6+j] = nflist[i][j];
  643.         workbuf[6+j] = '\0';
  644.         fprintf2(obuf,"%s','%c'+80H\n",workbuf,nflist[i][j]);
  645.     }
  646.  
  647.     fputs2("\t\tDB\t0\n",obuf);
  648.  
  649.     if (verbose)
  650.         fputs2("\n; Length of body:\n",obuf);
  651.  
  652.     sprintf(undrbuf,"\t\tDW\t%s$END-$-2\n",cfunam);
  653.     fputnam (obuf, undrbuf);
  654.  
  655.     if (verbose)
  656.         fputs2("\n; Body:\n",obuf);
  657.  
  658.     sprintf(undrbuf,"%s$STRT\tEQU\t$\n",cfunam);
  659.     fputnam (obuf, undrbuf);
  660.     if (nfcount) {
  661.         sprintf(undrbuf,"%s$R%03d\tEQU\t$+1-%s$STRT\n",
  662.             cfunam,relblc++,cfunam);
  663.         fputnam (obuf, undrbuf);
  664.         sprintf(undrbuf,"\t\tJMP\t%s$STRTC-%s$STRT\n",cfunam,cfunam);
  665.         fputnam (obuf, undrbuf);
  666.     }
  667.     sprintf(undrbuf,"%s$EF$%s\tEQU\t%s$STRT\n",cfunam,cfunam,cfunam);
  668.     fputnam (obuf, undrbuf);
  669.     for (i=0; i < nfcount; i++) {
  670.         sprintf(undrbuf,"%s$EF$%s\tJMP\t0\n",cfunam,nflist[i]);
  671.         fputnam (obuf, undrbuf);
  672.         }
  673.     sprintf(undrbuf,"\n%s$STRTC\tEQU\t$\n",cfunam);
  674.     fputnam (obuf, undrbuf);
  675. }
  676.  
  677.  
  678. doreloc()
  679. {
  680.     int i;
  681.  
  682.     if(verbose)
  683.         fputs2("\n; Relocation parameters:\n",obuf);
  684.  
  685.     fprintf2(obuf,"\t\tDW\t%d\n",relblc);
  686.     condptr.cb = &condbuf;
  687.     for(i = 0; i < relblc; i++)
  688.         {    
  689.         while (*condptr.ci <= i) {
  690.             ++condptr.ci;
  691.             fputnam (obuf, condptr.cb);
  692.             condptr.cb += strlen (condptr.cb) + 1;
  693.             }
  694.         sprintf(undrbuf,"\t\tDW\t%s$R%03d\n",cfunam,i);
  695.         fputnam (obuf, undrbuf);
  696.         }
  697.     while (*condptr.ci != 0x7FFF) {
  698.         ++condptr.ci;
  699.         fputnam (obuf, condptr.cb);
  700.         condptr.cb += strlen (condptr.cb) + 1;
  701.         }
  702.     fputs2("\n",obuf);
  703. }
  704.  
  705.  
  706. putdir()
  707. {
  708.     int i,j, length;
  709.     int bytecount;
  710.  
  711.     bytecount = 0;
  712.  
  713.     fputs2("\n\t\tORG\tTPALOC\n\n; Directory:\n",obuf);
  714.     for (i = 0; i < fcount; i++) {
  715.         strcpy(workbuf,"\t\tDB\t'");
  716.         length = strlen(fnames[i]);
  717.         length = length < 8 ? length : 8;
  718.         for (j = 0; j < length - 1; j++)
  719.             workbuf[6+j] = fnames[i][j];
  720.         workbuf[6+j] = '\0';
  721.         fprintf2(obuf,"%s','%c'+80H\n",workbuf,fnames[i][j]);
  722.         sprintf(undrbuf,"\t\tDW\t%s$BEG\n",fnames[i]);
  723.         fputnam (obuf, undrbuf);
  724.         bytecount += (length + 2);
  725.     }
  726.     fputs2("\t\tDB\t80H\n\t\tDW\tEND$CRL\n",obuf);
  727.  
  728.     bytecount += 3;
  729.     if (bytecount > DIRSIZE) {
  730.         printf("CRL Directory size will exceed 512 bytes;\n");
  731.         printf("Break the file up into smaller chunks, please!\n");
  732.         exit(-1);
  733.     }
  734.  
  735.     fputs2("\n\n; External information:\n", obuf);
  736.     fputs2("\t\tORG\tTPALOC+0200H\n", obuf);
  737.     fputs2("\t\tIF\tSYS$EXTFLAG\n", obuf);
  738.     fputs2("\t\tDB\t0BDH\n", obuf);
  739.     fputs2("\t\tDW\tSYS$EXTADDR\n", obuf);
  740.     fputs2("\t\tENDIF\n", obuf);
  741.     fputs2("\t\tIF\tNOT SYS$EXTFLAG\n", obuf);
  742.     fputs2("\t\tDB\t0, 0, 0\n", obuf);
  743.     fputs2("\t\tENDIF\n", obuf);
  744.     fputs2("\t\tDW\tSYS$EXTSIZE\n", obuf);
  745.  
  746. }
  747.  
  748.  
  749. initequ()
  750. {
  751.     equtab[0] = "A";
  752.     equtab[1] = "B";
  753.     equtab[2] = "C";
  754.     equtab[3] = "D";
  755.     equtab[4] = "E";
  756.     equtab[5] = "H";
  757.     equtab[6] = "L";
  758.     equtab[7] = "M";
  759.     equtab[8] = "SP";
  760.     equtab[9] = "PSW";
  761.     equtab[10]= "AND";
  762.     equtab[11]= "OR";
  763.     equtab[12]= "MOD";
  764.     equtab[13]= "NOT";
  765.     equtab[14]= "XOR";
  766.     equtab[15]= "SHL";
  767.     equtab[16]= "SHR";
  768.     equcount = 14;
  769. }
  770.  
  771.  
  772. int isidchr(c)    /* return true if c is legal character in identifier */
  773. char c;
  774. {    
  775.     return isalpha(c) || c == '$' || isdigit(c) || c == '.' || c == '_';
  776. }
  777.  
  778.  
  779. int isidstrt(c)    /* return true if c is legal as first char of idenfitier */
  780. char c;
  781. {
  782.     return isalpha(c) || c == '_';
  783. }
  784.  
  785.  
  786. int streq(s1, s2)    /* return true if the two strings are equal */
  787. char *s1, *s2;
  788. {
  789.     if (*s1 != *s2) return 0;    /* special case for speed */
  790.     while (*s1) if (*s1++ != *s2++) return 0;
  791.     return (*s2) ? 0 : 1;
  792. }
  793.  
  794.  
  795. skip_wsp(strptr)    /* skip white space at *strptr and modify the ptr */
  796. char **strptr;
  797. {
  798.     while (isspace(**strptr)) (*strptr)++;
  799. }
  800.  
  801.  
  802. strcpy2(s1,s2)    /* copy s2 to s1, converting to upper case as we go */
  803. char *s1, *s2;
  804. {
  805.     while (*s2)
  806.          *s1++ = toupper(*s2++);
  807.     *s1 = '\0';
  808. }
  809.  
  810.  
  811. /*
  812.     General-purpose string-replacement function:
  813.         'string'    is pointer to entire string,
  814.         'insstr'    is pointer to string to be inserted,
  815.         'pos'        is the position in 'string' where 'insstr'
  816.                 is to be inserted
  817.         'lenold'    is the length of the substring in 'string'
  818.                 that is being replaced.
  819. */
  820.  
  821. replstr(string, insstr, pos, lenold)
  822. char *string, *insstr;
  823. {
  824.     int length, i, j, k, x;
  825.  
  826.     length = strlen(string);
  827.     x = strlen(insstr);
  828.     k = x - lenold;
  829.     i = string + pos + lenold;
  830.     if (k) movmem(i, i+k, length - (pos + lenold) + 1);
  831.     for (i = 0, j = pos; i < x; i++, j++)
  832.         string[j] = insstr[i];
  833. }
  834.  
  835. /*    Function to put text to the output file, replacing underscores with
  836.     cues to keep ASM happy.   */
  837.  
  838. fputnam (file, str)
  839.     FILE *file;
  840.     char *str;
  841.     {
  842.     char c;
  843.     char quoted;
  844.     quoted = FALSE;
  845.     while (c = *str++) {
  846.         if (c == '\'') quoted ^= 1;
  847.         else if (c == '\n') putc ('\r', file);
  848.         if (c == '_' && !quoted) putc ('Q', file);
  849.         else putc (c, file);
  850.         }
  851.     }
  852.  
  853. error(msg,arg1,arg2)
  854. char *msg;
  855. {
  856.     printf("\n\7%s: %d: ",cfilnam,lino);
  857.     printf(msg,arg1,arg2);
  858.     errf = 1;
  859. }
  860.  
  861.  
  862. abort(msg,arg1,arg2)
  863. char *msg;
  864. {
  865.     error(msg,arg1,arg2);
  866.     putchar('\n');
  867.     if (cbufp == incbuf) fclose(incbuf);
  868.     fclose(fbuf);
  869.     if (*SUBFILE)
  870.         unlink(SUBFILE);
  871.     exit(-1);
  872. }
  873.  
  874.  
  875. sbrk2(n)    /* allocate storage and check for out of space condition */
  876. {
  877.     int i;
  878.     if ((i = sbrk(n)) == ERROR)
  879.         abort("Out of storage allocation space\n");
  880.     return i;
  881. }
  882.  
  883. bumptxtp(str)    /* bump txtbufp by size of given string + 1 */
  884. char *str;
  885. {
  886.     txtbufp += strlen(str) + 1;
  887.     if (txtbufp >= txtbuf + (TXTBUFSIZE - 8))
  888.      abort("Out of text space. Increase TXTBUFSIZE and recompile CASM");
  889. }
  890.  
  891.  
  892. int norel(id)    /* return true if identifier is exempt from relocatetion */
  893. char *id;
  894. {
  895.     if (isequ(id)) return 1;
  896.     return 0;
  897. }
  898.  
  899.  
  900. int isequ(str)    /* return true if given string is in the EQU table */
  901. char *str;
  902. {
  903.     int i;
  904.     for (i = 0; i < equcount; i++)
  905.         if (streq(str,equtab[i]))
  906.             return 1;
  907.     return 0;
  908. }
  909.  
  910.  
  911. char *isef(str)    /* return nflist entry if given string is an external */
  912. char *str;    /* function name */
  913. {
  914.     int i;
  915.     for (i = 0; i < nfcount; i++)
  916.         if (streq(str,nflist[i]))
  917.             return nflist[i];
  918.     return 0;
  919. }
  920.  
  921. int hasdot(str)    /* return true if given string has a dot in it */
  922. char *str;
  923. {
  924.     while (*str)
  925.         if (*str++ == '.')
  926.             return TRUE;
  927.     return FALSE;
  928. }
  929.  
  930. fputs2(arg1,arg2)
  931. {
  932.     if (fputs(arg1,arg2) == ERROR)
  933.         abort("Out of disk space for output file.");
  934. }
  935.  
  936. fprintf2(arg1,arg2,arg3,arg4,arg5)
  937. {
  938.     if (fprintf(arg1,arg2,arg3,arg4,arg5) == ERROR)
  939.         abort("Out of disk space for output file.");
  940. }
  941. abort("Out of text space. Increase TXTBUFSIZE and recompile CASM");
  942. }
  943.  
  944.  
  945. int